package com.cavalier.surtr.commonBase;

import com.cavalier.surtr.enums.ErrorEnum;
import lombok.Data;

import java.io.Serializable;
import java.util.List;

/**
 */
@Data
public class ProcessResult<T> implements Serializable {
	private static final long serialVersionUID = 1L;

    /**分页返回列表*/
    private List<T> list;

	private int pageNo = BaseQuery.DEFAULT_PAGE;

	private int totalCount; // 总的数目

	private int pageSize = BaseQuery.DEFAULT_PAGE_SIZE; //每一页数目

	/**
	 * 返回的数据
	 */
	private T data;

	/**
	 * 是否成功
	 */
	private boolean isSuccess;

	/**
	 * 正确返回这里为空null，否则为错误信息
	 */
	private String errorMsg;

	/**
	 * 错误码code
	 */
	private String code;

	public static <T> ProcessResult<T> wrapSuccessfulResultForData(T data) {
		ProcessResult<T> result = new ProcessResult<>();
		result.data = data;
		result.isSuccess = true;
		return result;
	}

	public static <T> ProcessResult<T> wrapSuccessfulResultForList(List<T> data) {
		ProcessResult<T> result = new ProcessResult<>();
		result.list= data;
		result.isSuccess = true;
		return result;
	}

	public static <T> ProcessResult<T> wrapErrorResult(ErrorEnum error) {
		ProcessResult<T> result = new ProcessResult<>();
		result.isSuccess = false;
		result.code = error.getCode();
		result.errorMsg = error.getDesc();
		return result;
	}

	public static <T> ProcessResult<String> wrapErrorResult(String code, String message) {
		ProcessResult<String> result = new ProcessResult<>();
		result.isSuccess = true;
		result.code = code;
		result.errorMsg = message;
		result.data="falid";
		return result;
	}


	public int getTotalPage() {
        if(pageNo < 1){
            pageNo = BaseQuery.DEFAULT_PAGE;
        }
		return totalCount / pageSize + (totalCount % pageSize == 0 ? 0 : 1);
	}
}
